const http = require('../../../utils/http.js'); 
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgPath: 'https://mocar.img.xzozo.com/img/icon/home/',
    index: 0,
    index1: 0,
    index2: 0,
    baseData: '',
    array: '',
    array2: ['个人', '企业'],
    form: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  onShow(){
    this.getbase_data();
  },
  // 获取页面基础数据
  getbase_data() {
    http.postRequest('/mini/v1/car_owner/base_data',{},res=> {
      this.setData({
        baseData: res.data
      })
    })
  },
  // 添加客户
  addOwner() {
    let from = this.data.form;
    from.owner_type = this.data.index2;
    if(!this.data.form.name) {
      wx.showToast({
        title: '姓名不能为空',
        icon: "none"
      })
      return;
    }
    if(!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(this.data.form.tel))){
      wx.showToast({
        title: '手机号错误',
        icon: "none"
      })
      return;
    }
    if(!from.staff_id) { 
      from.staff_id = this.data.baseData.staff[0]['id']
    }
    if(!from.gender) { 
      from.gender = 0;
    }
    if(!from.shop) {
      from.shop_id = this.data.baseData.shop[0].id
    }
    from.mobile = from.tel;
    http.postRequest('/mini/v1/car_owner/add_owner',from,res=> {
      console.log(res.code);
      if(res.code == 0) {     
        wx.navigateBack();
      }
    })
  },
  radioChange(e) {
    let from = this.data.form;
    from.gender = e.detail.value;
    this.setData({
      form: from
    })
  },
  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value
    })
  },
  pickerIpu6: function (e) {
    let from = this.data.form;
    from.remark = e.detail.value;
    this.setData({
      form: from
    })
  },
  bindPickerChange1: function (e) {
    let from = this.data.form;
    from.staff_id = this.data.baseData.staff[e.detail.value]['id']
    this.setData({
      index1: e.detail.value,
      form: from
    })
  },
  bindPickerChange2: function (e) {
    this.setData({
      index2: e.detail.value
    })
  },
  pickerIpu1(e) {
    const val = e.detail.value;
    let form = this.data.form;
    form.name = val
    this.setData({
      form: form
    })
  },
  pickerIpu2(e) {
    const val = e.detail.value;
    let form = this.data.form;
    form.tel = val
    this.setData({
      form: form
    })
  },
  textnone(e) {
    const that = this;
    let form = this.data.form
    const len = Object.keys(form).length;
    if(len) {  
      wx.showModal({
        content: '您确定要清空吗？',
        success: function (res) {
          if (res.confirm) {
            for (let i in form) {
              form[i] = ''
            }
            that.setData({
              form: form
            })
          }
        }
      })
    }
  }
})